package Dao;

import UnderAccessDBclass.Druid_utils;
import UnderAccessDBclass.jdbcUtils;
import bean.User;

import java.util.ArrayList;

public class UserDaoImpl implements IUserDao {
    //初始化预编译对象，结果集，数据库连接
    ArrayList<Object[]> list = new ArrayList<Object[]>(); //存储结果集

    @Override
    public User find(User user) {//登录
        //获取连接
        User us = new User();
        String sql = "select * from users where username=? and password=?";
        Object[] params = new Object[2];
        params[0] = (Object) user.getUsername();
        params[1] = (Object) user.getPassword();
        list = Druid_utils.executeQuery(sql, params);
        if (list != null && list.size() > 0) {
            for (Object[] obj : list) {
                us.setUsername((String) obj[0]);
                us.setPassword((String) obj[1]);
            }
        }
        return null;
    }

    @Override
    public boolean registerUser(User user) {//注册
        //获取连接
        Object[] params = new Object[2];
        params[0] = (Object) user.getUsername();
        params[1] = (Object) user.getPassword();
        String sql = "insert into users(username,password) values(?,?)";
        return jdbcUtils.executeUpdate(sql, params);
    }
}
